
$(function(){
    $('#defaultForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
        	order:{
            	validators: {
                    notEmpty: {
                        message: '订单必填'
                    }
                }
            },
            expressNo:{
            	validators: {
                    notEmpty: {
                        message: '快递单号必填'
                    }
                }
            },
            phone:{
            	validators: {
                    notEmpty: {
                        message: '联系人必填'
                    }
                }
            }
        }
    }).on('success.form.bv', function(e) {
        // Prevent form submission
        e.preventDefault();

        // Get the form instance
        var $form = $(e.target);

        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');
        if(!confirm("务必要保证单号及联系方式填写正确，本功能不做数据校验.")){
        	$(":submit").removeAttr('disabled');
        	return false;
        }
        // Use Ajax to submit form data
        $.post($form.attr('action'), $form.serialize(), function(result) {
        	if(result.errorCode != 200){
        		alert(result.moreInfo);
        		return;
        	}else
        	  alert(commonSuccess);
        	  window.location.href = location.href;
        }, 'json');
    });

	$(".order").click(function(){
		$(".user").html("");
		$(".order_").html("");
		var orderNum = $("#order").val();
		$.post(ctx+"/order/express/ordershow",{
			orderNum:orderNum
		},function(result){
			if(result.errorCode == 200){
				if(result.data == null){
					alert("确认订单号吧，什么也没查到。");
				}else{
					var address = result.data.address;
					$(".user").html(address.consignee+":"+address.zone+address.detailAddress+"<span id=\"phone\" style=\"margin-left:10px;\">"+address.phone+"</span>");
					var storeList = result.data.storeList;
					var order_ = "<ul>";
					for(var i = 0 ; i< storeList.length;i++){
						order_ += "<li>"+storeList[i].name+"<span>("+storeList[i].id+")</span>";
						if(activeOrderWare(orderNum,storeList[i].id)){
							order_ += "<button class=\"btn btn-info wt\" data-toggle=\"modal\" onclick=\"express('"+storeList[i].name+"',"+storeList[i].id+")\">发 货</button>";
						}else{
							order_ += "<span style=\"color:red;margin-left:10px;\">已发货</span>";
						}
						var products = storeList[i].goodsList;
						for(var j = 0 ; j < products.length;j++){
							order_ += "<ul>";
							order_ += "<li>"+products[j].title;
							order_ += "</li>";
							order_ += "</ul>";
						}
						order_ += "</li>";
					}
					order_ += "</ul>";
					$(".order_").append(order_);
				}
			}else{
				alert("确认订单号吧，什么也没查到。");
			}
		});
	});
});

function express(wareHouseName,wareHouseId){
	$("#orderNum").val($("#order").val());
	$("#warehouse").html(wareHouseName);
	$("#contract").val($("#phone").html());
	$("#wareHouseId").val(wareHouseId);
	$('#myModal').modal('show');
}

function activeOrderWare(orderNum,warehouseId){
	var tmp = false;
	$.ajaxSetup({
        async: false
    });
	$.post(ctx+"/order/express/warehouse",{
		orderNum:orderNum,
		warehouseId:warehouseId
	},function(result){
		tmp = result.data;
	})
	return tmp;
}
